package eu.webhib.extgui.update;

import java.util.*;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 * Controls the partial update of components by ajax means as provided by view framework (primeFaces).
 * This handles the needs of updating a tree and its interconnect list
 */
public class TreeListUpdateMngr extends UpdateMngr {

   private static final Logger log = Logger.getLogger(TreeListUpdateMngr.class);
   /** list of components to be updated with this UpdateManager */
   private List<String> selectiveUpdates = new ArrayList(2);

   /**
    * Tree and list components are to be updated
    * @param treeComponentId Id string of the tree be updated
    * @param listComponentId Id string of the list to be updated
    */
   public TreeListUpdateMngr(String treeComponentId, String listComponentId) {
      /** implicit ordering: in the super-constructor first parameter needs to be added first to the list */
      super(treeComponentId, listComponentId);
   }

   /**
    * Take a list of component Ids
    * @param componentList list of component Id string to be updated
    */
   public TreeListUpdateMngr(List<String> componentList) {
      super(componentList);
   }

   /**
    * mark the tree component for update
    */
   public void markTreeUpdate() {
      assert (this.size() > 0);
      selectiveUpdates.add(this.get(0));
   }

   /**
    * mark the list component for update
    */
   public void markListUpdate() {
      assert (this.size() > 1);
      selectiveUpdates.add(this.get(1));
   }

   /**
    * Set the components to be updated as marked before. Update means to be returned to the browser.
    */
   public void updateMarkedTargets() {
      logOut ("updateMarkedTargets: ", selectiveUpdates);

      /** set the components to be updated, thus returned to the browser */
      RequestContext.getCurrentInstance().addPartialUpdateTargets(selectiveUpdates);
      /** Clean up, so this object may be reused afterwards */
      selectiveUpdates.removeAll(selectiveUpdates);
   }
}
